package com.example.api.service.impl;

import com.example.api.model.entity.*;
import com.example.api.model.vo.CommodityChartVo;
import com.example.api.repository.*;
import com.example.api.service.DistributionService;
import com.example.api.service.InventoryRecordService;
import com.example.api.service.InventoryService;
import com.example.api.utils.DataTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DistributionServiceImpl implements DistributionService {

    @Resource
    private DistributionRepository distributionRepository;

    @Resource
    private DriverRepository driverRepository;

    @Resource
    private VehicleRepository vehicleRepository;

    @Resource
    private InventoryRepository inventoryRepository;


    @Autowired
    private InventoryRecordService recordService;


    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private SaleRepository saleRepository;

    @Override
    public Distribution save(Distribution distribution) throws Exception {
        if (distributionRepository.findById(distribution.getId()).isEmpty()) {
            Optional<Driver> driver = driverRepository.findById(distribution.getDid());
            Optional<Vehicle> vehicle = vehicleRepository.findById(distribution.getVid());
            Optional<Commodity> commodity = commodityRepository.findById(distribution.getCid());
            if(commodity.isEmpty()) throw new Exception("货物不存在");

            InventoryRecord inventoryRecord = new InventoryRecord();
            inventoryRecord.setCid(distribution.getCid());
            inventoryRecord.setWid(distribution.getWid());
            inventoryRecord.setName(commodity.get().getName());
            inventoryRecord.setCount(distribution.getCount());
            inventoryRecord.setType(-1);
            inventoryRecord.setCreateAt(DataTimeUtil.getNowTimeString());

            recordService.out(inventoryRecord);


            if (driver.isEmpty() || vehicle.isEmpty()) throw new Exception("请求参数错误");
            if (driver.get().isDriving() || vehicle.get().isDriving()) throw new Exception("司机或货车状态不可用");
            driverRepository.updateDriving(true, distribution.getDid());
            vehicleRepository.updateDriving(true, distribution.getVid());

        }
        if(distribution.getStatus()==1){
            if(distribution.getSid()!=null){
                Optional<Sale> sale = saleRepository.findById(distribution.getSid());
                if(sale.isEmpty()) throw new Exception("销售单不存在");
                saleRepository.updateStatus(1, distribution.getSid());
            }
        }
        if(distribution.getStatus()==2){
            driverRepository.updateDriving(false, distribution.getDid());
            vehicleRepository.updateDriving(false, distribution.getVid());
            if(distribution.getSid()!=null){
                Optional<Sale> sale = saleRepository.findById(distribution.getSid());
                if(sale.isEmpty()) throw new Exception("销售单不存在");
                saleRepository.updateStatus(2, distribution.getSid());
            }
        }
        else if(distribution.getStatus()==3){
            driverRepository.updateDriving(false, distribution.getDid());
            vehicleRepository.updateDriving(false, distribution.getVid());
            if(distribution.getSid()!=null){
                Optional<Sale> sale = saleRepository.findById(distribution.getSid());
                if(sale.isEmpty()) throw new Exception("销售单不存在");
                saleRepository.updateStatus(4, distribution.getSid());
            }
        }
        return distributionRepository.save(distribution);
    }



    @Override
    public List<Distribution> findAll() {
        return distributionRepository.findAll();
    }

}
